/*************************************************
Author: will
Date: 8/23/2012
Description:
Notes: 
*************************************************/

#ifndef __CBattleMap_h__
#define __CBattleMap_h__

#include "commclass.h"
#include "CTArray.h"

class CGridUnit
{
public:
    CGridUnit() 
    {
        Clear();
    }

    void Clear()
    {
        _ucCareer = 0;
        _ucOrder = 0;
        _iRoleID = -1;
    }
    
    virtual ~CGridUnit() 
    {
    }

    int GetRoleID() const {return _iRoleID;}
    unsigned char GetCareer() const {return _ucCareer;}
    unsigned char GetAtkOrder() const {return _ucOrder;}

    void SetRoleID(int iRoleID){_iRoleID = iRoleID;}
    void SetCareer(unsigned char ucCareer){_ucCareer = ucCareer;}
    void SetOrder(unsigned char ucOrder){_ucOrder = ucOrder;}

private:
    unsigned char _ucCareer;   //职业
    unsigned char _ucOrder;    //出手
    int _iRoleID;               
};

typedef CTLib::CTArray<CGridUnit, MAX_BATTLE_ROLES_PER_SIDE> MAP_GRID_MULTI;

class CGridMulti
{
public:
    enum BATTLE_GRID_OWNER
    {
        BATTLE_GRID_OWNER_NONE,
        BATTLE_GRID_OWNER_LEFT,
        BATTLE_GRID_OWNER_RIGHT,
    };

    CGridMulti() 
    {
        Clear();
    }

    void Clear()
    {
        _ucOwner = BATTLE_GRID_OWNER_NONE;
        _astGridMulti.Clear();
    }

    virtual ~CGridMulti() 
    {
    }

    unsigned char GetOwner() const {return _ucOwner;}
    void SetOwner(unsigned char ucOwner) {_ucOwner = ucOwner;}

    int GetGridMultiSize() const {return _astGridMulti.Size();}
    int GetGridMultiRoles(MAP_GRID_MULTI& stMapGridMulti);
    int GetGridMultiSideRoles(MAP_GRID_MULTI& stMapGridMulti, bool bLeftSide);
    int GetGridMultiCareerRoles(MAP_GRID_MULTI& stMapGridMulti,
            unsigned char ucCareer);

    int GetGridMultiPlayer(MAP_GRID_MULTI& stMapGridMulti);
    int GetGridMultiPartner(MAP_GRID_MULTI& stMapGridMulti);

    int LeaveGrid(int iRoleID, CGridUnit& stGridUnit);
    int GotoGrid(CGridUnit& stGridUnit);

    bool IsGridRoleAllDead();

private:
    unsigned char _ucOwner;
    CTLib::CTArray<CGridUnit, MAX_BATTLE_GRIDS> _astGridMulti;
};

class CMapSide
{
public:
    CMapSide() 
    {
    }

    virtual ~CMapSide() 
    {
    }

    int InitMapSide(bool bLeftSide);

    int ReCalcBorder();

    int Move(int iRoleID, int iFrom, int iTo);

    bool IsValidPos(int iPos){ return iPos >= 0 && iPos < MAX_BATTLE_GRIDS;}

    unsigned char GetGridOwner(int iPos);

    int GetLeftFrontTo() const {return _iLeftFrontTo;}
    int GetLeftBackTo() const {return _iLeftBackTo;}
    int GetRightFrontTo() const {return _iRightFrontTo;}
    int GetRightBackTo() const {return _iRightBackTo;}

    int GetDistanceRoles(int iSrcPos, int iDstPos,
            MAP_GRID_MULTI& stMapGridMulti);

    int GetDistanceSideRoles(int iSrcPos, int iDstPos,
            MAP_GRID_MULTI& stMapGridMulti, bool bLeftSide);

    int GetDistanceCareerRoles(int iSrcPos, int iDstPos,
            MAP_GRID_MULTI& stMapGridMulti, unsigned char ucCareer);

    int GetDistancePlayer(int iSrcPos, int iDstPos,
            MAP_GRID_MULTI& stMapGridMulti);

    int GetDistancePartner(int iSrcPos, int iDstPos,
            MAP_GRID_MULTI& stMapGridMulti);

private:
    int _iLeftFrontTo;          //左方前场边界
    int _iLeftBackTo;           //左方后场边界
    int _iRightFrontTo;         //右方前场边界
    int _iRightBackTo;          //右方后场边界
    CTArray<CGridMulti, MAX_BATTLE_GRIDS> _astSideGrid;
};

class CBattleMap
{
public:
    CBattleMap() 
    {
    }

    virtual ~CBattleMap() 
    {
    }

    CMapSide& GetMapSide(bool bIsLeft)
    {
        return bIsLeft ? _stLeftSide : _stRightSide;
    }

    int InitBattleMap();

    int ReCalcBorder();

    int MoveTo(int iRoleID, int iFrom, int iTo, bool bLeftSide,
            bool bNeedRecord);

    bool IsValidPos(int iPos){return _stLeftSide.IsValidPos(iPos);}

private:
    CMapSide _stLeftSide;
    CMapSide _stRightSide;
};

#endif // __CBattleMap_h__

